package com.controller.app;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiAction;
import com.apidoc.annotation.ApiParam;
import com.apidoc.annotation.ApiReqParams;
import com.apidoc.annotation.ApiRespParams;
import com.apidoc.enumeration.DataType;
import com.apidoc.enumeration.Method;
import com.apidoc.enumeration.ParamType;
import com.service.IFileMetaService;
import com.utils.commons.ResponseBean;
import com.utils.enums.ENUM_SUCESS;

@Api(name = "文件上传", order = 1, mapping = "/wx")
@Controller
@RequestMapping("/wx")
public class WxFileUploadController {
	
	@Autowired
	private IFileMetaService fileMetaService;
	
	
	@ApiAction(name = "文件上传", order = 1, mapping = "/fileUpload", method = Method.POST)
	@ApiReqParams(type = ParamType.URL_PARAM, value = {
			@ApiParam(name = "userId", dataType = DataType.NUMBER, required = false, defaultValue = "", description = "人员id"),
			@ApiParam(name = "type", dataType = DataType.NUMBER, required = false, defaultValue = "", description = "人员类型"),
			@ApiParam(name = "file", dataType = DataType.FILE, defaultValue = "", description = "文件")
    })
    @ApiRespParams({
            @ApiParam(name = "status", dataType = DataType.NUMBER, defaultValue = "", description = "状态码"),
            @ApiParam(name = "data", dataType = DataType.OBJECT, defaultValue = "", description = "响应数据", object="map"),
            @ApiParam(name = "ossUrl", dataType = DataType.OBJECT, defaultValue = "", description = "oss地址", belongTo="map"),
            @ApiParam(name = "fileName", dataType = DataType.STRING, defaultValue = "", description = "原文件名", belongTo="map"),
            @ApiParam(name = "cdnUrl", dataType = DataType.OBJECT, defaultValue = "", description = "cdn地址", belongTo="map"),
            @ApiParam(name = "text", dataType = DataType.STRING, defaultValue = "", description = "提示信息")
    })
	@CrossOrigin(origins = "*")
	@RequestMapping(value = "/fileUpload", produces = "text/html;charset=UTF-8")
	public void ueditImageUpload(@RequestParam(value = "file", required = false)MultipartFile[] file, HttpServletResponse response,
			Long userId, Integer type) throws IOException {
        if(file == null) {
        	return;
        }
        List<Object> list = fileMetaService.uplordFile(file, userId, type);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(ResponseBean.of(list, ENUM_SUCESS.SUCESS.value));
        
	}
	
	@ApiAction(name = "文件上传（直接返回地址版）", order = 1, mapping = "/fileUploadSimple", method = Method.POST)
	@ApiReqParams(type = ParamType.URL_PARAM, value = {
			@ApiParam(name = "userId", dataType = DataType.NUMBER, required = false, defaultValue = "", description = "人员id"),
			@ApiParam(name = "type", dataType = DataType.NUMBER, required = false, defaultValue = "", description = "人员类型"),
			@ApiParam(name = "file", dataType = DataType.FILE, defaultValue = "", description = "文件")
    })
    @ApiRespParams({
    		@ApiParam(name = "直接给oss地址", dataType = DataType.STRING, defaultValue = "")
    })
	@CrossOrigin(origins = "*")
	@RequestMapping(value = "/fileUploadSimple", produces = "text/html;charset=UTF-8")
	public void fileUploadSimple(@RequestParam(value = "file", required = false)MultipartFile[] file, HttpServletResponse response,
			Long userId, Integer type) throws IOException {
        if(file == null) {
        	return;
        }
        List<Object> list = fileMetaService.uplordFile(file, userId, type);
        @SuppressWarnings("unchecked")
		Map<String,String> ossmap = (Map<String, String>) list.get(0);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(ossmap.get("ossUrl"));
	}
	
}
